﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Sicurezza.CA.Entita;
using Sicurezza.CA.DataProvider;
using OpenSSL.X509;
using OpenSSL.Core;
using System.Configuration;

namespace Sicurezza.CA.Manager
{
    public class X509Manager
    {
        private static CertificateDataProvider dataProvider = new CertificateDataProvider();

        public static string Get(string certId)
        {
            string cert = "";
            try
            {
                cert = FileManager.LoadCertificate(Convert.ToInt32(certId));
            }
            catch (FileNotFoundException ex)
            {
                return null;
            }

            return cert;
        }

        

        public static List<Certificate> GetAll()
        {
            return dataProvider.GetAll();
        }

        public static string GetView(string Id)
        {
            string cert = Get(Id);
            X509Certificate c = new X509Certificate(new BIO(cert));
            return c.ToString();
        }

        public static X509Certificate GetCaCertificate()
        {
            string cert = FileManager.LoadCaCert();
            return new X509Certificate(new BIO(cert));
        }
    }
}
